extends Control
@onready var label: Label = $Label

const LINES := [
	"骑士找回了自己的记忆",
	"曾经的辉煌夹杂着遗憾洗刷着他的身躯",
	"桥由他亲手断开...",
	"也由他亲自重塑",
	"渡忆之桥·完",
]

var current_line := -1
var tween: Tween
var show_death_count := false

func _ready() -> void:
	show_line(0)

func _input(event: InputEvent) -> void:	
	if tween and tween.is_running():
		return
	
	if (
		event is InputEventKey or 
		event is InputEventJoypadButton or 
		event is InputEventMouseButton
	):
		if event.is_pressed() and not event.is_echo():
			if show_death_count:
				# 如果正在显示死亡次数，返回主菜单
				SceneManage.go_to_main_menu()
			elif current_line + 1 < LINES.size():
				show_line(current_line + 1)
			else:
				# 所有对话结束，显示死亡次数
				Show_death_count()

func show_line(line: int) -> void:
	current_line = line
	tween = create_tween()
	tween.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_SINE)
	
	if line > 0:
		tween.tween_property(label, "modulate:a", 0, 1)
	else:
		label.modulate.a = 0
	
	tween.tween_callback(label.set_text.bind(LINES[line]))
	tween.tween_property(label, "modulate:a", 1, 1)

func Show_death_count() -> void:
	show_death_count = true
	
	tween = create_tween()
	tween.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_SINE)
	
	# 淡出当前文本
	tween.tween_property(label, "modulate:a", 0, 1)
	
	# 切换到死亡次数文本
	tween.tween_callback(func(): 
		label.text = "死亡次数: " + str(get_death_count())
	)
	
	# 淡入死亡次数文本
	tween.tween_property(label, "modulate:a", 1, 1)

func get_death_count() -> int:
	return SceneManage.get_global_death_count()
